﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DVDFilmoteka.src.Exporters;
using System.IO;

namespace DVDFilmoteka.Forms
{
    public partial class ExporterForm : Form
    {
        private readonly List<MovieStorage.MovieDataStore12> database;

        public ExporterForm(List<MovieStorage.MovieDataStore12> database)
        {
            InitializeComponent();

            this.database = database;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonExport_Click(object sender, EventArgs e)
        {
            try
            {
                if (saveFileDialog1.ShowDialog(this) == DialogResult.OK)
                {
                    String fileName = saveFileDialog1.FileName;
                    String extension = fileName.Substring(fileName.LastIndexOf('.') + 1);

                    IDatabaseExporter exporter = ExporterFactory.GetExporter(extension);
                    if (exporter != null)
                    {
                        progressBar.Visible = true;
                        this.Cursor = Cursors.WaitCursor;

                        MovieStorage.MovieDataStore12 columns = new MovieStorage.MovieDataStore12();

                        columns.compression = checkBoxCompresion.Checked ? "" : null;
                        columns.container = checkBoxContainer.Checked ? "" : null;
                        columns.country = checkBoxCountry.Checked ? "" : null;
                        columns.csfdRating = checkBoxCSFDRating.Checked ? "" : null;
                        columns.czechName = checkBoxCzechname.Checked ? "" : null;
                        columns.directorName = checkBoxDirector.Checked ? "" : null;
                        columns.channelCount = checkBoxChannelCount.Checked ? "" : null;
                        columns.imdbRating = checkBoxIMDBRating.Checked ? "" : null;
                        columns.language = checkBoxLanguage.Checked ? "" : null;
                        columns.length = checkBoxLength.Checked ? "" : null;
                        columns.link = checkBoxLink.Checked ? "" : null;
                        columns.mainGenre = checkBoxMainGenre.Checked ? "" : null;
                        columns.medium = checkBoxMedium.Checked ? "" : null;
                        columns.mediumNotes = checkBoxMediumNotes.Checked ? "" : null;
                        columns.movieNotes = checkBoxFilmNotes.Checked ? "" : null;
                        columns.myRating = checkBoxMyRating.Checked ? "" : null;
                        columns.originalName = checkBoxOriginalname.Checked ? "" : null;
                        columns.quality = checkBoxQuality.Checked ? "" : null;
                        columns.recenze = checkBoxRecenze.Checked ? "" : null;
                        columns.resolution = checkBoxResolution.Checked ? "" : null;
                        columns.size = checkBoxSize.Checked ? "" : null;
                        columns.year = checkBoxYear.Checked ? "" : null;                        

                        exporter.ColumnsToExport = columns;
                        exporter.Database = database;
                        exporter.FileName = fileName;

                        backgroundWorker1.RunWorkerAsync(exporter);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Během exportu došlo k chybě! Vyjímka: " + ex.Message, "Chyba exportu", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            IDatabaseExporter exporter = e.Argument as IDatabaseExporter;
            
            if (File.Exists(exporter.FileName)) File.Delete(exporter.FileName);
            exporter.Export();            
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            progressBar.Visible = false;
            this.Cursor = Cursors.Arrow;
            this.Close();
        }

        /// <summary>
        /// Oznaceni/odoznaceni zatrzitka
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkBox_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxCompresion.Checked || checkBoxContainer.Checked || checkBoxCountry.Checked || checkBoxCSFDRating.Checked || checkBoxCzechname.Checked || checkBoxDirector.Checked || checkBoxFilmNotes.Checked || checkBoxChannelCount.Checked || checkBoxIMDBRating.Checked || checkBoxLanguage.Checked || checkBoxLength.Checked || checkBoxLink.Checked || checkBoxMainGenre.Checked || checkBoxMedium.Checked || checkBoxMediumNotes.Checked || checkBoxMyRating.Checked || checkBoxOriginalname.Checked || checkBoxQuality.Checked || checkBoxRecenze.Checked || checkBoxResolution.Checked || checkBoxSize.Checked || checkBoxYear.Checked)
            {
                buttonExport.Enabled = true;
            }
            else
            {
                buttonExport.Enabled = false;
            }
        }
    }
}
